package 双周赛;

import java.util.PriorityQueue;

/**
 * @
 * @date 2024/03/02
 */
public class 第125场双周赛 {

    public static void main(String[] args) {

        //[97,24,43,45,45,26]
        //45
        第125场双周赛 impl = new 第125场双周赛();
        //[2,11,10,1,3]
        //10
        //[999999999,999999999,999999999]
        //1000000000
        System.out.println(impl.minOperations2(new int[]{999999999, 999999999, 999999999}, 1000000000));

    }

    public int minOperations(int[] nums, int k) {
        int ans = 0;
        for (int num : nums) {
            if (num < k) {
                ans++;
            }
        }

        return ans;
    }

    public int minOperations2(int[] nums, int k) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(
                (a, b) -> a - b
        );
        for (int num : nums) {
            if (num < k) {
                priorityQueue.add(num);
            }
        }

        int ans = 0;

        while (priorityQueue.size() > 1) {
            Integer x = priorityQueue.poll();
            Integer y = priorityQueue.poll();

            long next = (long) Math.min(x, y) * 2 + Math.max(x, y);
            if (next < k) {
                priorityQueue.add((int) next);
            }
            ans++;
        }

        if (!priorityQueue.isEmpty() && priorityQueue.peek() < k) {
            ans++;
        }
        return ans;
    }

}
